package org.yagnus.scalasupport.iterates

object BreakableBoolean {
  final class BreakableBoolean(b: => Boolean) {
    private var notBroken = true;
    @inline final def apply = get;
    @inline final def get = notBroken && b;
    @inline final def break { notBroken = false }
    @inline final def breakable = this;
    @inline final def makeBreakable = this;
    @inline final def allowBreak = this;
    @inline final def allowBreaks = this;
  }
  @inline final implicit def makeBooleanBreakable(b: => Boolean) = new BreakableBoolean(b);
  @inline final implicit def breakableBooleanToOrig(brb: BreakableBoolean): Boolean = brb.get;

}

object DemobreakableBoolean extends App {
  import BreakableBoolean._;
  var i = 0;
  //  var t = new BreakableBoolean(i < 100);
  var t = i < 100 makeBreakable;
  //just to make sure it breaks original condition
  while (t) {
    i += 1;
    println(i);
  }

  //now check if it breaks at a smaller number
  i = 0;
  while (t) {
    i += 1;
    println(i);
    if (i > 5) t.break;
  }
}

